# -*- coding: utf-8 -*-

from droidguide_classes import *
from map.controlers import *

import os
import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template


class MainHandler(webapp.RequestHandler):

  def get(self):
    self.response.out.write('Mapa Main.py')

class Route:
	lat = None
	lng = None
	lat2 = None
	lng2 = None

class ShowRoute(webapp.RequestHandler):
    def get(self):
		landmarks = Landmark.all()
		l0 = landmarks[0]
		route = "from: %s,%s " % (l0.lat, l0.lon)

		for l in landmarks[1:landmarks.count()]:
			route = route + "to: %s, %s "  % (l.lat, l.lon)

		userid = self.request.get('userid')
		template_values = {'userid':userid,'landmarks': landmarks, 'userid': userid, 'route':route}
		path = os.path.join(os.path.dirname(__file__), 'templates/map_route.html')
		self.response.out.write(template.render(path, template_values))

class ShowRouteForUser(webapp.RequestHandler):
    def generateRouteForLandmarks(landmarks):
                if landmarks:
		    l0 = landmarks[0]
		    route = "from: %s,%s " % (l0.lat, l0.lon)

		    for l in landmarks[1:landmarks.count()]:
			route = route + "to: %s, %s "  % (l.lat, l.lon)
                    return route
    
    def getLandmarksForUser(droidGuideUser_key):
		    L = []
                    q = UserActivity.gql("WHERE droidGuideUser_key = :dgkey",
                             dgkey=droidGuideUser_key)
	            results = q.fetch(100)
		    if q and results:
		        for userActivity in results:
		            landmark_key = userActivity.activity_key
			    landmark = getObject(landmark_key)
			    if landmark:
			        L.append(landmark)
		    return L

    def get(self):
                droidGuideUser_key = self.request.get('droidGuideUser_key')
		L = []
		if droidGuideUser_key:
                    L = getLandmarksForUser(droidGuideUser_key)
                    route = generateRouteForLandmarks(L)
		else:
		    L = Landmark.all()
		    route = generateRouteForLandmarks(L)
                if route:
  		    userid = self.request.get('userid')
		    template_values = {'userid':userid,'landmarks': landmarks, 'userid': userid, 'route':route}
		    path = os.path.join(os.path.dirname(__file__), 'templates/map_route.html')
		    self.response.out.write(template.render(path, template_values))
		else:
		    self.response.out.write('No landmarks were found in the system')

class ShowMap(webapp.RequestHandler):
    def get(self):
		landmarks = Landmark.all()
		userid = self.request.get('userid')

		overlays = ""
		if self.request.get('wikipedia'): 
			overlays = overlays + "map.addOverlay(wikipedia)\n"
		if self.request.get('panoramio'): 
		    overlays = overlays + "map.addOverlay(panoramio)\n"

		template_values = {'userid':userid,'landmarks': landmarks, 'userid': userid, 'overlays': overlays}
		path = os.path.join(os.path.dirname(__file__), 'templates/map.html')
		self.response.out.write(template.render(path, template_values))


def main():
  application = webapp.WSGIApplication([
      ('/map/all', ShowMap),
	  ('/map/route', ShowRoute),
      ('/map/(\w+)/all.kml', LandmarkControler.KMLLandmarks),   #Gera kml com todos landmarks para um usuário
      ('/map/landmark/add', LandmarkControler.Add),
      ('/map/add_landmark', LandmarkControler.AddForm),
      ('/map/', MainHandler)],
      debug=True)
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()
